package com.cf.data.serve.environment;

import com.cf.data.serve.entity.HjjcData;
import com.cf.data.serve.service.HjjcDataService;
import com.cf.data.serve.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class TCPServer {


    //123
/*    ##0290ST=43 系统编号
    CN=2011 命令编号
    PW=123456 访问密码
    MN=HIK30039975648 设备唯一标 识
    CP=&&DataTime=20240902113500 指令参数
    PM25-Rtd=15,PM25-Flag=N PM2.5
    PM10-Rtd=30,PM10-Flag=N PM10
    ZY-Rtd=38.2,ZY-Flag=N
    TEM-Rtd=30.1,TEM-Flag=N 温度
    RH-Rtd=40.4,RH-Flag=N 湿度
    WS-Rtd=0.0,WS-Flag=N 风速
    WD-Rtd=329,WD-Flag=N 风向
    a01006-Rtd=100.65,a01006-Flag=N 气压
    a34001-Rtd=37,a34001-Flag=N&&3AC0 TSP
    */
    private static HjjcDataService hjjcDataService = BeanUtils.getBean(HjjcDataService.class);

    public static void main(String[] args) throws IOException {
        jc(8234);
    }

    public static void jc(int port) throws IOException {
        ServerSocket serverSocket = null;
        try {
            System.out.println("开启端口：8334");
            serverSocket = new ServerSocket(port); // 监听1234端口
            Socket clientSocket = serverSocket.accept(); // 等待客户端连接
            System.out.println("连接成功");

            // 获取输入流
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // 获取输出流
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                //将获取到的数据分开
                String[] s = inputLine.split(";");
                for(int i = 0;i<s.length;i++){

                    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +":"+s[i]);
                }
                //System.out.println(inputLine); // 将接收到的消息发送回客户端

                if(s.length>=14){
                    //将数据和状态分开
                    String[] z = null;
                    HjjcData hjjcSbgl = new HjjcData();
                    hjjcSbgl.setId(String.valueOf(System.currentTimeMillis()));
                    hjjcSbgl.setState("0");
                    hjjcSbgl.setAddTime(new Date());
                    hjjcSbgl.setMn(s[3].replace("MN=",""));
                    hjjcSbgl.setOverall(inputLine);
                    z = s[5].split(",");
                    hjjcSbgl.setPm25(z[0].replace("PM25-Rtd=",""));
                    hjjcSbgl.setPm25T(z[1].replace("PM25-Flag=",""));
                    z = s[6].split(",");
                    hjjcSbgl.setPm10(z[0].replace("PM10-Rtd=",""));
                    hjjcSbgl.setPm10T(z[1].replace("PM10-Flag=",""));
                    z = s[7].split(",");
                    hjjcSbgl.setZy(z[0].replace("ZY-Rtd=",""));
                    hjjcSbgl.setZyT(z[1].replace("ZY-Flag=",""));
                    z = s[8].split(",");
                    hjjcSbgl.setTem(z[0].replace("TEM-Rtd=",""));
                    hjjcSbgl.setTemT(z[1].replace("TEM-Flag=",""));
                    z = s[9].split(",");
                    hjjcSbgl.setRh(z[0].replace("RH-Rtd=",""));
                    hjjcSbgl.setRhT(z[1].replace("RH-Flag=",""));
                    z = s[10].split(",");
                    hjjcSbgl.setWs(z[0].replace("WS-Rtd=",""));
                    hjjcSbgl.setWsT(z[1].replace("WS-Flag=",""));
                    z = s[11].split(",");
                    hjjcSbgl.setWd(z[0].replace("WD-Rtd=",""));
                    hjjcSbgl.setWdT(z[1].replace("WD-Flag=",""));
                    z = s[12].split(",");
                    hjjcSbgl.setPre(z[0].replace("a01006-Rtd=",""));
                    hjjcSbgl.setPreT(z[1].replace("a01006-Flag=",""));
                    z = s[13].split(",");
                    hjjcSbgl.setTsp(z[0].replace("a34001-Rtd=",""));
                    hjjcSbgl.setTspT(z[1].replace("a34001-Flag=",""));
                    hjjcDataService.save(hjjcSbgl);
                    if(serverSocket != null){
                        System.out.println("客户端已断开连接。");
                    }
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null);
            serverSocket.close();
            System.out.println("客户端已断开连接。");
            jc(port);
        }

    }

}
